<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body> 
  <canvas id="canvas" height="400" width="400"></canvas>
</body>
<script>
  var canvas = document.getElementById('canvas'); //获取canvas标签
  var ctx = canvas.getContext('2d'); //获取上下文

  canvas.style.border = '2px solid #000';

  //绘制图片
  var img = new Image(); //创建图片DOM对象
  img.src = 'zoulu.jpg'; //图片路径，设置后img对象会立即加载图片
  img.onload = function () {
    var framIndex = 0;
    //循环计时器 setInterval
    setInterval(function () {
      //清除之前裁剪的所有元素,只展示最新的元素，实现动态效果
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.drawImage(img, framIndex * 80, 0, 50, 400, 100, 100, 80, 300); //绘制图片
      framIndex++; //添加到下一帧
      framIndex %= 5;
    }, 1000 / 6);
  }
</script>


</html>